package com.shanghaionstar.discover.geofence.presenter;

import android.content.Intent;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.GeofenceInfoEntity;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.geofence.GeofenceInfoActivity;
import com.shanghaionstar.discover.geofence.iview.IGeofenceHomeView;
import com.shanghaionstar.discover.geofence.service.GeofenceService;
import com.shanghaionstar.enumtype.AlertType;

import java.util.List;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by 7828wc on 16/7/21.
 */
public class GeofenceHomePresenter extends BasePresenter<GeofenceInfoEntity> {
    private GeofenceService service;
    private IGeofenceHomeView view;

    public GeofenceHomePresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<GeofenceInfoEntity> view) {
        super.setView(view);
        service = new GeofenceService(view.onActivity());
        this.view = (IGeofenceHomeView) view;
    }

    public void getGeofencing() {
        view.setLoading(true);
        subscription = service.init().flatMap(new Func1<String, Observable<List<GeofenceInfoEntity>>>() {
            @Override
            public Observable<List<GeofenceInfoEntity>> call(String entity) {
                return service.getGeofencing();
            }
        }).subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Action1<List<GeofenceInfoEntity>>() {
                    @Override
                    public void call(List<GeofenceInfoEntity> geofenceList) {
                        if (geofenceList != null && geofenceList.size() > 0) {
                            view.onReponseSuccess(geofenceList.get(0));
                        } else {
                            view.onReponseSuccess(null);
                        }
                        view.setLoading(false);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //抛出Error异常
                        view.onResponseError((OnStarException) throwable);
                        view.setLoading(false);
                    }
                });

    }

    public void setRule(GeofenceInfoEntity entity){
        if (!CommonUtil.isNull(entity.getAlertType())) {
            if (entity.getAlertType().equals(AlertType.Out.value())) {
                entity.setRule(view.onActivity().getString(R.string.out_geofence));
            } else {
                entity.setRule(view.onActivity().getString(R.string.in_geofence));
            }
        } else {
            entity.setRule(AlertType.All.name());
        }
    }

    public void launchGeofenceInfo(){
        Intent it = new Intent(view.onActivity(), GeofenceInfoActivity.class);
        view.onActivity().startActivity(it);
        view.onActivity().finish();
    }
}
